<template>
  <div class="app-container">
    <!-- *************************************搜索区************************************* -->
    <!-- 分类 -->
    <PageSearch :showSearch.sync="showSearch" :getList="getList" :handleQuery="handleQuery" :resetQuery="resetQuery">
      <template #pagination>
        <pagination
          v-show="pageTotal > 0"
          :total="pageTotal"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </template>
      <template #form>
        <el-form v-show="showSearch" :model="queryParams" ref="queryForm" size="small" :inline="true">
          <el-row>
            <el-form-item prop="skuNo">
              <el-input
                :placeholder="$t('sku编号')"
                v-model="queryParams.skuNo"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item prop="distrName">
              <el-input
                :placeholder="$t('供应商名称')"
                v-model="queryParams.distrName"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <!-- 时间选择 -->
            <el-form-item>
              <el-date-picker
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                v-model="queryParams.time"
                type="daterange"
                value-format="yyyy-MM-dd"
              />
            </el-form-item>
            <el-form-item prop="applyStatus">
              <el-select @change="getList" :placeholder="$t('审批状态')" v-model="queryParams.applyStatus">
                <el-option :label="$t('待审批')" :value="1"></el-option>
                <el-option :label="$t('审批通过')" :value="2"> </el-option>
                <el-option :label="$t('拒绝')" :value="3"> </el-option>
              </el-select>
            </el-form-item>

            <el-form-item prop="distrName">
              <el-select :placeholder="$t('申请者')" v-model="queryParams.distrId">
                <el-option v-for="(item, index) in sellerListData" :label="item.name" :value="item.distrId"></el-option>
              </el-select>
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item prop="payStatus">
              <el-select :placeholder="$t('付款状态')" v-model="queryParams.payStatus">
                <el-option :label="$t('支付完成')" value="2"></el-option>
                <el-option :label="$t('部分支付')" value="1"></el-option>
                <el-option :label="$t('未支付')" value="0"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item prop="skuNameCn">
              <el-input :placeholder="$t('中文名')" v-model="queryParams.skuNameCn" clearable style="width: 240px" />
            </el-form-item>
            <el-form-item prop="skuNameJp">
              <el-input
                :placeholder="$t('日文名')"
                v-model="queryParams.skuNameJp"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-row>
        </el-form>
      </template>

      <template #actions>
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-button
              type="primary"
              :disabled="tableCheck.length == 0"
              plain
              v-hasPermi="['system:skuSellerApply:check']"
              size="mini"
              @click="handleBatchAudit"
            >
              {{ $t('批量审批') }}
            </el-button>
          </el-col>
        </el-row>
      </template>
    </PageSearch>

    <el-table
      @row-click="(row) => $rowClick(row, clickedRows)"
      :row-class-name="({ row }) => $tableRowClassName(row, clickedRows)"
      :height="calTableHeight()"
      @selection-change="(rows) => (tableCheck = rows)"
      v-loading="loading"
      ref="TableRef"
      :data="tableData"
      row-key="menuId"
    >
      <el-table-column :selectable="(row) => row.applyStatus == 1" type="selection" width="55" align="center" />

      <!-- sku -->
      <el-table-column align="center" show-overflow-tooltip prop="skuNo" :label="$t('sku编号')" width="150">
        <template slot-scope="scope">
          <div @click="handleEdit(scope.row)" style="cursor: pointer; color: #1890ff">
            {{ scope.row.skuNo }}
          </div>
        </template>
      </el-table-column>
      <!-- 主图 -->
      <el-table-column :label="$t('主图')" prop="imgUrl" align="center">
        <template slot-scope="scope">
          <el-image
            :key="Math.random()"
            v-hover="scope.row.imgUrl"
            style="width: 50px; height: 50px"
            fit="contain"
            :src="scope.row.imgUrl + '?x-oss-process=image/resize,w_100/quality,q_70'"
          />
        </template>
      </el-table-column>
      <!-- 申请时价格 -->
      <el-table-column :label="$t('申请时价格')" prop="applyPrice" align="center">
        <template slot-scope="scope">
          {{ (+scope.row.applyPrice || 0).toFixed() }}
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip prop="cloudPrice" :label="$t('供应商价格')">
        <template slot-scope="scope">
          {{ (+scope.row.cloudPrice || 0).toFixed() }}
        </template>
      </el-table-column>

      <el-table-column align="center" show-overflow-tooltip prop="applyNum" :label="$t('申请数量')" />
      <el-table-column align="center" show-overflow-tooltip prop="useNum" :label="$t('剩下可用数量')" />
      <el-table-column align="center" show-overflow-tooltip prop="saleNum" :label="$t('出售数量')" />
      <el-table-column align="center" show-overflow-tooltip prop="payNum" :label="$t('付款数量')" />
      <el-table-column show-overflow-tooltip :label="$t('退回数量')" align="center" prop="backNum" />
      <el-table-column show-overflow-tooltip :label="$t('申请日期')" align="center" prop="applyTime" width="150" />
      <el-table-column show-overflow-tooltip :label="$t('审批状态')" align="center" prop="applyStatus">
        <template slot-scope="scope">
          <span style="color: rgb(24, 144, 255)" v-if="scope.row.applyStatus == 1"> {{ $t('待审批') }}</span>
          <span style="color: #13ce66" v-if="scope.row.applyStatus == 2"> {{ $t('审批通过') }}</span>
          <span style="color: #ff4949" v-if="scope.row.applyStatus == 3"> {{ $t('拒绝') }}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip :label="$t('审批备注')" align="center" prop="handleRemark" />
      <el-table-column show-overflow-tooltip :label="$t('申请者')" align="center" prop="distrName" width="150" />
      <el-table-column show-overflow-tooltip :label="$t('付款完成状态')" align="center" prop="payStatus" width="150">
        <template slot-scope="scope">
          <span style="color: #ff4949" v-if="scope.row.payStatus == 0"> {{ $t('未支付') }}</span>
          <span style="color: rgb(24, 144, 255)" v-if="scope.row.payStatus == 1"> {{ $t('部分支付') }}</span>
          <span style="color: #13ce66" v-if="scope.row.payStatus == 2"> {{ $t('支付完成') }}</span>
        </template>
      </el-table-column>

      <el-table-column fixed="right" :label="$t('操作')" width="150" align="center">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:skuSellerApply:check']"
            type="text"
            @click="handleAudit(scope.row)"
            v-show="scope.row.applyStatus == 1"
          >
            {{ $t('审批') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="pageTotal > 0"
      :total="pageTotal"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 申请 -->
    <el-dialog
      :visible.sync="editDialogConfig"
      title="审批备注"
      top="10vh"
      width="400px"
      destroy-on-close
      :modal="true"
      :close-on-click-modal="false"
      class="articleModal"
    >
      <div>
        <el-form ref="elForm" :model="curentData" :rules="curentDataRuler" size="small" label-width="100px">
          <el-row>
            <el-col :span="24">
              <el-form-item :label="$t('审批备注')" prop="curentDataBachRemark">
                <el-input
                  v-model="curentData.curentDataBachRemark"
                  type="textarea"
                  placeholder="请输入审批备注"
                  clearable
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row> </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>

    <!-- sku详情/修改 -->
    <Skucontmoduler @relaod="relaod" ref="Skucontmoduler"></Skucontmoduler>
  </div>
</template>

<script>
import { disGetSkuSellerApplyList, disHandleSellerApply, sellerList } from '@/api/distribution/sku/SupplierLpproval'
import Skucontmoduler from '@/components/Skucontmoduler'

export default {
  activated() {
    this.$nextTick(() => this.$refs['TableRef']?.doLayout())
  },
  name: 'SupplierLpproval',
  data() {
    return {
      /**点击的表格项 */
      clickedRows: [],
      showSearch: true,
      sellerListData: [],

      curentData: {
        curentDataBachRemark: '',
        label: '',
        skuId: '',
        skuNo: '',
        imgUr: '',
        skuNameCn: '',
        skuNameJp: '',
        cloudStock: '',
        cloudPrice: '',
        distrName: '',
        updateTime: '',
        remark: '',
      },
      editDialogConfig: false,
      tableData: [],
      tableCheck: [],
      /**数据总条数 */
      pageTotal: 0,
      loading: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        value2: 1,
        applyStatus: 1,
        time: [],
      },
      curentDataRuler: {
        curentDataBachRemark: [{ required: true, message: '审批备注必传', trigger: 'blur' }],
      },
    }
  },
  watch: {
    'queryParams.pageNum': function (val) {
      this.clickedRows = []
    },
  },
  components: {
    Skucontmoduler,
  },
  created() {
    this.getList()
  },
  mounted() {
    this.getSellerList()
  },
  methods: {
    handleBatchAudit() {
      this.$confirm(this.$t('是否通过卖家申请?'), this.$t('提示'), {
        confirmButtonText: this.$t('通过'),
        cancelButtonText: this.$t('取消'),
        type: 'warning',
        distinguishCancelAndClose: true,
      }).then(() => {
        const ids = this.tableCheck.map((i) => i.id)
        const data = ids.map((i) => ({ id: i, status: 1 }))
        disHandleSellerApply(data).then((res) => {
          this.$message({ type: 'success', message: res.msg })

          this.getList()
        })
      })
    },
    // 获取申请者下拉
    getSellerList() {
      sellerList().then((res) => {
        this.sellerListData = res.data
      })
    },
    relaod() {
      this.getList()
    },
    handleEdit(row) {
      this.$refs.Skucontmoduler.setSkuInfo(row)
      this.$refs.Skucontmoduler.checkNumber('basicInformation')
    },

    // 取消申请
    cancel() {
      this.editDialogConfig = !this.editDialogConfig
    },
    // 提交申请
    submitForm() {
      this.$refs.elForm.validate((valid) => {
        if (valid) {
          disHandleSellerApply([
            {
              id: this.curentData.id,
              status: 2,
              handleRemark: this.curentData.curentDataBachRemark,
            },
          ]).then((res) => {
            this.$message({
              type: 'success',
              message: this.$t('操作成功'),
            })
            this.cancel()
            this.getList()
          })
        }
      })
    },
    /**审批 */
    handleAudit(data) {
      this.curentData = data
      this.$set(this.curentData, 'curentDataBachRemark', '')
      this.$confirm(this.$t('是否通过卖家申请?'), this.$t('提示'), {
        confirmButtonText: this.$t('通过'),
        cancelButtonText: this.$t('拒绝'),
        type: 'warning',
        distinguishCancelAndClose: true,
      })
        .then(() => {
          disHandleSellerApply([{ id: this.curentData.id, status: 1 }]).then((res) => {
            this.$message({
              type: 'success',
              message: this.$t('操作成功'),
            })
            this.getList()
          })
        })
        .catch((flage) => {
          console.log(flage)
          if (flage !== 'close') {
            this.editDialogConfig = !this.editDialogConfig
          }
        })
    },
    /** 查询菜单列表 */
    getList() {
      /****************** 处理查询条件 *********************/
      const selectDate = this.queryParams.time || []
      const applyStartTime = selectDate[0] || ''
      const applyEndTime = selectDate[1] || ''
      const data = {
        ...this.queryParams,
        applyStartTime,
        applyEndTime,
      }
      delete data.time
      /**************** 处理查询条件 end *******************/
      this.loading = true
      disGetSkuSellerApplyList(data).then((res) => {
        this.tableData = res.rows
        this.pageTotal = res.total
        this.loading = false
      })
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.value1 = null
      Object.keys(this.queryParams).forEach((key) => {
        this.queryParams[key] = null
      })
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 10

      this.getList()
    },

    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false
      this.isExpandAll = !this.isExpandAll
      this.$nextTick(() => {
        this.refreshTable = true
      })
    },
  },
}
</script>
<style lang="scss" scoped>
.goods {
  &-img {
    &-box {
      position: relative;
    }

    &-mini {
      height: 50px;
      border-radius: 3px;

      &:hover {
        & + .goods-img-big {
          display: block;
        }
      }
    }

    &-big {
      // display: none;
      position: absolute;
      z-index: 100;
      top: -180px;
      left: 0;
      transform: translate(0, 70px);
      width: 200px;
      height: 200px;
      border-radius: 3px;
    }
  }
}

.bigImg {
  border-radius: 3px;
  z-index: 10;
  position: fixed;
  width: 200px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
</style>
